<include file="Public/header" title="地址管理" bdstyle="background:#f2f2f2"/>
<include file="Public/header_back"/>
    <article class="add-address">
    	<form id="dataForm">
    	<input type="hidden" name="address_id" value="{$address_data.address_id}"/>
        <div class="add-address-main">
            <section class="add-address-list flex-align">
                <span>联系人姓名</span>
               <input type="text" name="consignee" maxlength="13" placeholder="请输入联系人姓名" value="{$address_data.consignee}" class="flex-1"/>
            </section>
            <section class="add-address-list flex-align">
                <span>联系人电话</span>
               <input type="number" name="mobile" maxlength="11" placeholder="请输入联系人电话" value="{$address_data.mobile}" class="flex-1"/>
            </section>
            <section class="add-address-list flex-align">
                <span>邮政编码</span>
               <input type="number" name="zipcode" placeholder="请输入邮政编码" value="{$address_data.zipcode}" class="flex-1"/>
            </section>
            <section class="add-address-list flex-align">
                <span>电子邮件</span>
               <input type="text" name="email" placeholder="请输入电子邮件" value="{$address_data.email}" class="flex-1"/>
            </section>
            <style>
            	.address-section select{float:right;max-width:31%;margin-right:3%;direction: rtl;}
				.address-section select:first-child{margin-right:0;}
            </style>
            <section class="add-address-list flex-align">
                <span>省、市、区</span>
                <div class="flex-1 address-section clearfix">
		            <select name="district">
		                <option value="">地区</option>
		                <if condition="districts">
		            	<foreach name="districts" item="val">
		                <option value="{$val.region_id}" {:$address_data['district'] == $val['region_id']?'selected':''}>{$val.region_name}</option>
	                	</foreach>
	                	</if>
	                </select>
	                <select name="city" onchange="getDistricts(this)">
	                	<option value="">城市</option>
	                	<if condition="citys">
	                	<foreach name="citys" item="val">
	                	<option value="{$val.region_id}" {:$address_data['city'] == $val['region_id']?'selected':''}>{$val.region_name}</option>
		            	</foreach>
		            	</if>
		            </select>
	                <select name="province" onchange="getCitys(this)">
	                	<option value="">省份</option>
		                <foreach name="provinces" item="val">
		                <option value="{$val.region_id}" {:$address_data['province'] == $val['region_id']?'selected':''}>{$val.region_name}</option>
		                </foreach>
	                </select>
                </div>
            </section>
            <section class="add-address-list add-address-info">
                <h4>详细地址</h4>
                <textarea name="address" placeholder="请输入详细地址">{$address_data.address}</textarea>
            </section>
        </div>
        </form>
    </article> 
    <div class="entrue-add">
        <a href="javascript:saveData()">确认新增</a>
    </div>
<script>
function saveData(){
	$.post(
		"{:U('Ucenter/editAddress')}",
		$('#dataForm').serializeArray(),
		function(data){
			if(data.status == '1'){
				layer.msg(data.info)
				setTimeout("location.href='"+data.url+"'",1000)
			}else{
				layer.msg(data.info)
			}
		}
	)
}
function getCitys(obj){
	var province = $(obj).val(); 
	if(province == ''){
		$('select[name=city]').html('<option value="">城市</option>');
		$('select[name=district]').html('<option value="">地区</option>');
		return false;
	}
	$.post(
		"{:U('getCitys')}",
		{province:province},
		function(data){
			if(data.status == '1'){
				var citys = data.info.citys;
				var districts = data.info.districts;
				var citystr='';
				var districtstr = '';
				for(var i=0;i<citys.length;i++){
					citystr += '<option value="'+citys[i].region_id+'">'+citys[i].region_name+'</option>';
				}
				for(var i=0;i<districts.length;i++){
					districtstr += '<option value="'+districts[i].region_id+'">'+districts[i].region_name+'</option>';
				}
				if(citystr != ''){
					$('select[name=city]').html(citystr)
				}else{
					$('select[name=city]').html('<option value="">城市</option>');
				}
				if(districtstr != ''){
					$('select[name=district]').html(districtstr)
				}else{
					$('select[name=district]').html('<option value="">地区</option>');
				}
			}else{
				layer.msg(data.info);
			}
		}
	)
}
function getDistricts(obj){
	var city = $(obj).val();
	if(city == ''){
		$('select[name=district]').html('<option value="">地区</option>');
		return false;
	}
	$.post(
		"{:U('getDistricts')}",
		{city:city},
		function(data){
			if(data.status == '1'){
				var districtstr = '';
				var districts = data.info;
				for(var i=0;i<districts.length;i++){
					districtstr += '<option value="'+districts[i].region_id+'">'+districts[i].region_name+'</option>';
				}
				if(districtstr != ''){
					$('select[name=district]').html(districtstr)
				}else{
					$('select[name=district]').html('<option value="">地区</option>');
				}
			}else{
				layer.msg(data.info);
			}
		}
	)
}
</script>
<include file="Public/footer"/>




